package xyz.xtt.exchange.vo;

import static xyz.xtt.common.asset.AssertUtils.isNull;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import xyz.xtt.asset.api.enums.StatusAllEnum;
import xyz.xtt.asset.client.response.AssetAccountResponse;
import xyz.xtt.asset.client.response.AssetDetailInfo;
import xyz.xtt.asset.client.response.AssetExtInfoResponse;
import xyz.xtt.asset.client.response.AssetInfoResponse;
import xyz.xtt.asset.client.response.AssetStaticResponse;
import xyz.xtt.asset.client.response.TblAssetCardDto;
import xyz.xtt.asset.client.response.TblAssetNoDto;
import xyz.xtt.exchange.constants.ErrorMessage;
import xyz.xtt.exchange.constants.UpChainEnum;

/**
 * @ClassName：NftInterestDetailsVO
 * @Author: XGP
 * @Date: 2024/12/24/0024 9:52
 * @Desc:
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class NftInterestDetailsVo implements Serializable {
	@Serial
	private static final long serialVersionUID = 1L;
	// 资产基本信息 ------------------------------start

	/**
	 * 资产id
	 */
	private Long id;
	/**
	 * 资产goodsId
	 */
	private String goodsId;
	/**
	 * nft名称
	 */
	private String assetName;
	/**
	 * nft描述
	 */
	private String description;
	/**
	 * nft计价单位
	 */
	private Integer unit;

	// 资产基本信息 ------------------------------end

	/**
	 * nft价格
	 */
	private BigDecimal price;

	/**
	 * nft数量
	 */
	private Integer amount;
	/**
	 * nft-分类标识
	 */
	private Integer firstType;
	/**
	 * nft-分类描述
	 */
	private String categoryDescription;
	/**
	 * 创作者
	 */
	private String authorId;
	/**
	 * 持有人
	 */
	private String ownerId;

	/**
	 * 合作者
	 */
	private String cooperationId;

	/**
	 * nft-collectionId
	 */
	private String collectionId;
	/**
	 * nft-collection描述
	 */
	private String collectionDescription;
	/**
	 * nft-collection image
	 */
	private String coverImg;

	/**
	 * 作者头像
	 */
	private String authorAvatar;
	/**
	 * 作者昵称
	 */
	private String authorNickname;
	/**
	 * 拥有者头像
	 */
	private String ownerAvatar;
	/**
	 * 拥有者昵称
	 */
	private String ownerNickname;
	/**
	 * nft-展示图片
	 */
	private String logoUri;

	/**
	 * nft-tokenID
	 */
	private Long tokenId;
	/**
	 * 链id
	 */
	private String chainId;
	/**
	 * 链名称
	 */
	private String chainName;
	/**
	 * nft-hash
	 */
	private String chainTxHash;
	/**
	 * nft-协议
	 */
	private String referProtocol;

	private String assetNo;
	/**
	 * 发行方
	 */
	private String issuer;
	/**
	 * 发行状态 1 发行中 -1未发行
	 */
	@Builder.Default
	private String issuerState = "1";
	/**
	 * 发行日期
	 */
	private Long issuerDate;
	/**
	 * 流通类型
	 */
	private String flowType;
	/**
	 * 铸造商
	 */
	private String foundryMan;
	/**
	 * 铸造日期
	 */
	private Long foundryDate;
	/**
	 * 发行平台
	 */
	private String issuerPlatform;

	private int showType;

	private List<InterestVo> interestVoList;
	/**
	 * 区块链高度
	 */
	private String chainBlockNum;
	/**
	 * 上链时间
	 */
	private Long chainTime;
	/**
	 * 动态参数
	 */
	private String activeParam;

	private Integer upChain;
	private Long publicityPeriodStart;
	private Long publicityPeriodEnd;
	private Long preSellStartDatetime;
	private Long sellStartDatetime;
	private Long sellEndDatetime;

	/**
	 * 是否处于上链支付中
	 */
	private boolean upChainPaying;

	/** 群聊ID */
	private String imGroupId;
	/** 是否可以加群 */
	private Boolean isAddGroup;
	/** 是否可赠送 */
	private Boolean isGiveAway;
	private Integer secondType;
	private Integer sellPattern;

	private Integer fileType;
	private String fileUri;

	private String seqNo;

	/**
	 * 1155资产剩余数量
	 */
	private Integer goodsRemain;

	/**
	 * 是否可以上架
	 */
	private boolean chainOver5Days;
	/**
	 * 是否结束完整公示期
	 */
	private boolean publicityPeriod;

	// 证书地址
	private String certificateUri;

	private List<TblAssetCardDto> cardList = new ArrayList<>();

	private List<TblAssetNoDto> assetNoList = new ArrayList<>();

	/**
	 * 购买的最高价格
	 */
	private String maxPrice;
	private String maxPriceNERemintType;
	/**
	 * 当前登陆人图像
	 */
	private String currAvatar;
	private Long currentDataTime;

	private Integer ownerType;

	private Boolean personal;

	private Boolean firstBusiness;

	private Long assetAccountId;

	private Long assetNoId;
	private List<BenefitVO> assetBenefitList;
	private List<String> fileList;

	public static NftInterestDetailsVo build(AssetDetailInfo assetBaseInfo) {
		AssetInfoResponse asset = assetBaseInfo.getAsset();
		AssetStaticResponse assetStaticResource = assetBaseInfo.getAssetStatic();
		AssetExtInfoResponse assetExt = assetBaseInfo.getAssetExt();
		AssetAccountResponse assetAccount = assetBaseInfo.getAssetAccount();
		isNull(asset, ErrorMessage.ASSET_NOT_EXIST);
		isNull(assetAccount, ErrorMessage.ASSET_NOT_EXIST);
		isNull(assetExt, ErrorMessage.ASSET_NOT_EXIST);
		isNull(assetStaticResource, ErrorMessage.ASSET_NOT_EXIST);
		Integer firstType = asset.getFirstType();
		Integer secondType = asset.getSecondType();

		String seqNo = "";
		if (Objects.nonNull(assetExt.getSeqNo()) && !Objects.equals(0L, assetExt.getSeqNo())) {
			seqNo = String.format("%08d", assetExt.getSeqNo());
		}
		Integer sellPattern = assetAccount.getSellPattern();
		if (Objects.nonNull(asset.getSellable()) && Objects.equals(asset.getSellable(), StatusAllEnum.NO.code())) {
			sellPattern = 0;
		}

		return NftInterestDetailsVo
				.builder()
				.id(asset.getId())
				.goodsId(asset.getGoodsId())
				.assetName(asset.getAssetName())
				.description(asset.getDescription())
				.unit(asset.getUnit())
				.price(assetAccount.getPrice())
				.amount(asset.getAmount())
				.firstType(firstType)
				.authorId(asset.getAuthorId())
				.ownerId("" + assetAccount.getOwnerId())
				.logoUri(assetStaticResource.getLogoUri())
				.tokenId(assetExt.getTokenId())
				.chainId(assetExt.getChainId())
				.chainName(assetExt.getChainName())
				.chainTxHash(assetExt.getChainTxHash())
				.referProtocol(assetExt.getReferProtocol())
				.chainBlockNum(assetExt.getChainBlockNum())
				.chainTime(assetExt.getChainTime())
				.showType(assetExt.getShowType())
				.upChain(Optional.ofNullable(assetExt.getUpChain()).orElse(UpChainEnum.NO.code()))
				.publicityPeriodStart(assetExt.getPublicityPeriodStart())
				.publicityPeriodEnd(assetExt.getPublicityPeriodEnd())
				.preSellStartDatetime(assetExt.getPreSellStartDatetime())
				.sellStartDatetime(assetAccount.getLastPutOnSaleDatetime())
				.sellEndDatetime(assetExt.getSellEndDatetime())
				.issuerDate(asset.getCreateDatetime())
				.foundryDate(asset.getCreateDatetime())
				.isGiveAway(false)
				.sellPattern(sellPattern)
				.secondType(secondType)
				.seqNo(seqNo)
				.fileType(assetStaticResource.getFileType())
				.fileUri(assetStaticResource.getFileUri())
				.ownerType(asset.getOwnerType())
				.certificateUri(assetStaticResource.getCertificateUri())
				.assetAccountId(assetAccount.getId())
				.build();
	}
}
